package club.haomoon.boss.controller.system;

import club.haomoon.boss.service.system.IJobsService;
import club.haomoon.boss.validate.base.IdValidate;
import club.haomoon.boss.validate.base.PageValidate;
import club.haomoon.boss.validate.system.JobsCreateValidate;
import club.haomoon.boss.validate.system.JobsSearchValidate;
import club.haomoon.boss.validate.system.JobsUpdateValidate;
import club.haomoon.boss.vo.system.JobsDataVo;
import club.haomoon.core.aop.FreeValidator;
import club.haomoon.core.common.AjaxResult;
import club.haomoon.core.common.PageResult;
import club.haomoon.core.enums.ErrorEnum;
import club.haomoon.core.util.StringUtils;
import club.haomoon.core.validator.annotation.IDMust;
import com.alibaba.fastjson2.JSONObject;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/adminapi/dept.jobs")
@Tag(name =  "系统岗位管理")
public class JobsController {

    @Resource
    IJobsService iJobsService;

    @FreeValidator
    @GetMapping("/all")
    @Operation(summary="所有岗位")
    public AjaxResult<List<JobsDataVo>> all() {
        List<JobsDataVo> list = iJobsService.all();
        return AjaxResult.success(list);
    }

    @GetMapping("/lists")
    @Operation(summary="岗位列表")
    public AjaxResult<Object> list(@Validated PageValidate pageValidate,
                                               @Validated JobsSearchValidate searchValidate) {
        if (StringUtils.isNotNull(searchValidate.getExport()) && searchValidate.getExport().equals(1)) {
            JSONObject ret = iJobsService.getExportData(pageValidate, searchValidate);
            return AjaxResult.success(ret);
        } else if (StringUtils.isNotNull(searchValidate.getExport()) && searchValidate.getExport().equals(2)) {
            String path = iJobsService.export(searchValidate);
            return AjaxResult.success(2, new JSONObject() {{
                put("url", path);
            }}, ErrorEnum.SHOW_MSG.getCode());
        } else {
            PageResult<JobsDataVo> list = iJobsService.list(pageValidate, searchValidate);
            return AjaxResult.success(list);
        }
    }

    @GetMapping("/detail")
    @Operation(summary="岗位详情")
    public AjaxResult<JobsDataVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        JobsDataVo vo = iJobsService.detail(id);
        return AjaxResult.success(vo);
    }

    @PostMapping("/add")
    @Operation(summary="岗位新增")
    public AjaxResult<Object> add(@Validated @RequestBody JobsCreateValidate createValidate) {
        iJobsService.add(createValidate);
        return AjaxResult.success();
    }

    @PostMapping("/edit")
    @Operation(summary="岗位编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody JobsUpdateValidate updateValidate) {
        iJobsService.edit(updateValidate);
        return AjaxResult.success();
    }

    @PostMapping("/delete")
    @Operation(summary="岗位删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iJobsService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
